﻿/*
--------------------------------------
File: DecisionTreeMain.cs
Project: Themaopdracht 7
Author: Roel Blaauwgeers

Description:
Used to check an bitmap against the tree. Still uses setpixel for testing purposes!

NOT ready for implementation,
 - still uses setpixel
 - cannot process 32bpp bitmaps
--------------------------------------
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

namespace THO7_Team8
{
    class DecisionTreeMain : VisionAlgorithm
    {
        public DecisionTreeMain(String name)
            : base(name)
        {
            hasConfiguration = false;
        }

        public override System.Drawing.Bitmap DoAlgorithm(System.Drawing.Bitmap sourceImage)
        {
            
            //Load the tree from a file
            DecisionTree tree = (DecisionTree)Serializer.deSerialize("C:\\dtree.dat");

            Bitmap bmp = new Bitmap(sourceImage);

            //convert to 24bpp, this is a quick fix that should be deleted in future versions
            //TODO: fix this elsewhere;) 
            Bitmap pic1 = new Bitmap(bmp.Width, bmp.Height, PixelFormat.Format24bppRgb);
            Graphics g = Graphics.FromImage(pic1);
            g.DrawImage(bmp, 0, 0);
            g.Dispose();



            //Get the HSV-values from the test image
            HSV[,] hsvColors = DTUtils.BitmapToArray(pic1);
            
            //For all pixels 
            int patch_dimension = 5;
            for (int y = 0; y < pic1.Height; y++)
            {
                for (int x = 0; x < pic1.Width; x++)
                {
                    //Check if pixel is a foreground pixel
                    if (!tree.isForegroundPixel(DTUtils.getFeatures(x, y, pic1.Width, pic1.Height, hsvColors, patch_dimension)))
                    {
                        //If not make it white, TODO: DO NOT USE SETPIXEL!!!
                        pic1.SetPixel(x, y, Color.White);
                    }
                }
            }
  
            
            return pic1;
        }


    }
}






